﻿using CSUtils;
using G02;
using G02.TripleJump.UI;
using U3dUtils;
using UnityEngine;

namespace TripleJump {
    public class GameManage_TripleJump : CSingleton<GameManage_TripleJump> {
        CSceneManage sceneManage = new CSceneManage();
        CFirstPlayerInput firstPlayerInput = new CFirstPlayerInput();

        public void _Init() {
            sceneManage.Init();
            UIManage_TripleJump.GetInstance._Init();
            CFirstPlayerInput.CPara.onGameStart = GameStart;
            CFirstPlayerInput.CPara.onFingerClick = sceneManage.fristRole.ClickFly;
            CFirstPlayerInput.CPara.onFingerLift = sceneManage.fristRole.FingerLift;
            CFirstPlayerInput.CPara.onGetVitalityValue = delegate() { return 55; };
            CFirstPlayerInput.CPara.onUIAddVitalityEvent = delegate(long ff, Vector3 pos) { };
            CFirstPlayerInput.CPara.onSetGameRuningSpeed = SetGameRuningSpeed;

            TripleJump_FristRoleFly.CPara.onSetFlyState += UI_Help.SetFlyState;
            TripleJump_FristRoleFly.CPara.onRoleDeath += RoleDeath;
            TripleJump_FristRoleFly.CPara.onJumpQuality += UI_GameRuning.JumpQuality;

            UI_Help.CPara.onSetIsHelpStep += sceneManage.fristRole.SetIsHelpStep;
            UI_Help.CPara.onSetIsHelpStep += sceneManage.camControl.SetIsHelpStep;
            CMaxDistanceTips.CPara.onShowNewRecordTips += UI_Tips.ShowNewRecordTips;

            UI_Checkout.CPara.onGameReset = GameReset;

            firstPlayerInput.Init();
            CGpCard.GetInstance()._Init(10, 300);

            CDelayMain.Invoke(delegate {
                GameReset();
            });
        }

        void GameReset() {
            Debug.LogWarningFormat("游戏事件______________________ S_GameReset");
            TJGameData.gameState = EGameState.e01_GameReset;
            roleDeathCount = 0;
            UIManage_TripleJump.GetInstance.GameReset();
            sceneManage.GameReset();
            firstPlayerInput.GameReset();
        }

        /// <summary>
        /// 游戏准备
        /// </summary>
        void GameReady() {
            TJGameData.gameState = EGameState.e06_gameReady;
        }

        void GameStart() {
            Debug.LogWarningFormat("游戏事件______________________ S_GameStart");
            TJGameData.gameState = EGameState.e02_GameRuning;
            sceneManage.GameStart();
            UIManage_TripleJump.GetInstance.GameStart();
            CJetpack.CPara.onEnergyChangeEvent = UI_GameRuning.instance.EnergyProgress;
            firstPlayerInput.GameStart();
        }

        void GameEnd() {
            Debug.LogWarningFormat("游戏事件______________________ S_GameEnd");
            TJGameData.gameState = EGameState.e05_GameEnd;
            sceneManage.GameEnd();
            UIManage_TripleJump.GetInstance.GameEnd();
            firstPlayerInput.GameEnd();
        }

        void SetGameRuningSpeed(EGameRuningSpeedType slow) {
        }

        int roleDeathCount;

        void RoleDeath(int objId, int flyCount, int distance) {
            roleDeathCount++;
            // Debug.LogErrorFormat("_角色死亡_________{0}____{1}_____{2}_________{3}", objId, flyCount, distance, roleDeathCount);
            #if plus
            if (Part_10026_FristRoleFly.C_Parameter.o_fristRoleId == objId&& flyCount!=4) {
                C_ObjBase.mono.S_Delayed(1).d_EndEvent = delegate () {
                    S_GameEnd();
                    C_UIManage.GetInstance.uI_GameEndRanking.Open(false,o_sceneManage.roleArray,o_sceneManage.fristRole.currentFlyDistance);
                };
            }else if (roleDeathCount == o_sceneManage.roleArray.Length) {
                if (Part_10026_FristRoleFly.C_Parameter.o_fristRoleId == objId && flyCount != 4) {
                    C_ObjBase.mono.S_Delayed(1).d_EndEvent = delegate () {
                        S_GameEnd();
                        C_UIManage.GetInstance.uI_GameEndRanking.Open(true, o_sceneManage.roleArray, o_sceneManage.fristRole.currentFlyDistance);
                    };
                } else {
                    S_GameEnd();
                    C_UIManage.GetInstance.uI_GameEndRanking.Open(true, o_sceneManage.roleArray, o_sceneManage.fristRole.currentFlyDistance);
                }
            }

            #else
            if (TripleJump_FristRoleFly.CPara.firstRoleId == objId) {
                CDelayMain.Delay(1.8f).onCallback = delegate {
                    GameEnd();
                    //C_UIManage.GetInstance.uI_Checkout.Open(UI_Checkout.E_RewradType.e01_Checkout, (int)distance, null);
                    UI_Checkout.Open(UI_Checkout.ERewardType.e01_Checkout, (int)distance);
                };
            }
            #endif
        }
    }
}
